package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 根据名称分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        //判断是否携带参数
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        //定义分页对象
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());

        //定义查询对象
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();

        //判断参数是否为空
        if (null != dto.getName()) {
            wrapper.like(AdSensitive::getSensitives, dto.getName());
        }

        //查询数据
        IPage<AdSensitive> iPage = page(page, wrapper);

        //定义分页响应对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());

        //响应查询结果
        result.setData(iPage.getRecords());

        return result;
    }

    /**
     * 新增敏感词
     *
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult insertSensitive(AdSensitive sensitive) {

        //判断参数是否为空
        if (null == sensitive) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //补充创建时间
        sensitive.setCreatedTime(new Date());

        //存入数据库
        boolean save = save(sensitive);

        //判断是否保存成功
        if (save) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.DATA_EXIST);
    }

    /**
     * 修改敏感词
     *
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive sensitive) {
        //判断参数是否为空
        if (null == sensitive && null == sensitive.getId()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //修改数据
        boolean updateById = updateById(sensitive);

        //判断是否修改成功
        if (updateById) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //判断参数是否为空
        if (null == id && id <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //删除数据
        boolean removeById = removeById(id);

        //判断是否删除成功
        if (removeById) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
}
